import {createSlice, configureStore} from '@reduxjs/toolkit';

// 创建切片
const countSlice = createSlice({
    name:'count',
    initialState:{
        num:1
    },
    reducers:{// 定义程序员
        addNum(state, {payload}){
            state.num += payload
        },
        decNum(state, {payload}){
            state.num -= payload
        }
    }
})
const {addNum, decNum} = countSlice.actions;

console.log('countSlice : ', countSlice);

// 创建数据仓库
const store = configureStore({
    reducer:{
        count:countSlice.reducer
    }
})

// 如果仓库的数据变化了，能够监听到这个变化，进行响应的处理
// subscribe函数执行后会有一个返回值，返回值也是一个函数，是取消监听的函数
const unsubscribe = store.subscribe(()=>{
    console.log(store.getState());
})

store.dispatch(decNum(2));
unsubscribe();
store.dispatch(addNum(3));
